﻿#ifndef USERWORKER_H
#define USERWORKER_H

#include <QObject>
#include "UserEntity.h"
enum UserOptID{
    Opt_Login = 0,
    Opt_Register,
    Opt_ModifyPwd,
    Opt_ModifyInfo,
    Opt_Delete,
};

class UserWorker : public QObject
{
    Q_OBJECT
public:
    explicit UserWorker(QString databaseType = "QSQLITE", QString databaseName = "database.db", QObject *parent = nullptr);
    ~UserWorker();
public slots:
    void onInit();
public:
    ///base operate
    QJsonObject readUser(const QString& name);
    bool updateUser(const QString& name,const QJsonObject& data);
    bool deleteUser(const QString& name);
    bool createUser(const QString& name,const QJsonObject& data);
    ///end base operate
    QJsonArray readAllUser();
    bool renameUser(const QString& oldName,const QString& newName);
private:
    QJsonObject find(const QString& name);
    void createSuperAdmin();
    void createBaseUser();
    void createAdmin();
    void createRoot();
protected:
    IDatabase* database;
    QString m_tableName;
    QString m_databaseType;
    QString m_databaseName;

public slots:
    void onOperate(UserOptID optID,QJsonObject optObj);
signals:
    void sglOperateResult(UserOptID optID,bool result,QJsonObject optObj);
    void sglAllUser(QJsonArray allUser);

};

#endif // USERWORKER_H
